package service

import (
	"bloggo/structs"
	"context"
	"errors"
	"fmt"
	"gitee.com/chunanyong/logger"
	"gitee.com/chunanyong/zorm"
)

//SaveTbUserInfoStruct 保存
//如果入参ctx中没有dbConnection,使用defaultDao开启事务并最后提交
//如果入参ctx有dbConnection且没有事务,调用dbConnection.begin()开启事务并最后提交
//如果入参ctx有dbConnection且有事务,只使用不提交,有开启方提交事务
//但是如果遇到错误或者异常,虽然不是事务的开启方,也会回滚事务,让事务尽早回滚
func SaveTbUserInfoStruct(ctx context.Context, tbuserinfostruct *structs.TbUserInfoStruct) error {
	// tbuserinfostruct对象指针不能为空
	if tbuserinfostruct == nil {
		return errors.New("tbuserinfostruct对象指针不能为空")
	}
	//匿名函数return的error如果不为nil,事务就会回滚
	_, errSaveTbUserInfoStruct := zorm.Transaction(ctx, func(ctx context.Context) (interface{}, error) {

		//事务下的业务代码开始

		_, errSaveTbUserInfoStruct := zorm.Insert(ctx, tbuserinfostruct)

		if errSaveTbUserInfoStruct != nil {
			return nil, errSaveTbUserInfoStruct
		}

		return nil, nil
		//事务下的业务代码结束

	})

	//记录错误
	if errSaveTbUserInfoStruct != nil {
		errSaveTbUserInfoStruct := fmt.Errorf("service.SaveTbUserInfoStruct错误:%w", errSaveTbUserInfoStruct)
		logger.Error(errSaveTbUserInfoStruct)
		return errSaveTbUserInfoStruct
	}

	return nil
}

//UpdateTbUserInfoStruct 更新
//如果入参ctx中没有dbConnection,使用defaultDao开启事务并最后提交
//如果入参ctx有dbConnection且没有事务,调用dbConnection.begin()开启事务并最后提交
//如果入参ctx有dbConnection且有事务,只使用不提交,有开启方提交事务
//但是如果遇到错误或者异常,虽然不是事务的开启方,也会回滚事务,让事务尽早回滚
func UpdateTbUserInfoStruct(ctx context.Context, tbuserinfostruct *structs.TbUserInfoStruct) error {

	// tbuserinfostruct对象指针或主键Id不能为空
	if tbuserinfostruct == nil || (tbuserinfostruct.Id) < 1 {
		return errors.New("tbuserinfostruct对象指针或主键Id不能为空")
	}

	//匿名函数return的error如果不为nil,事务就会回滚
	_, errUpdateTbUserInfoStruct := zorm.Transaction(ctx, func(ctx context.Context) (interface{}, error) {

		//事务下的业务代码开始
		_, errUpdateTbUserInfoStruct := zorm.Update(ctx, tbuserinfostruct)

		if errUpdateTbUserInfoStruct != nil {
			return nil, errUpdateTbUserInfoStruct
		}

		return nil, nil
		//事务下的业务代码结束

	})

	//记录错误
	if errUpdateTbUserInfoStruct != nil {
		errUpdateTbUserInfoStruct := fmt.Errorf("service.UpdateTbUserInfoStruct错误:%w", errUpdateTbUserInfoStruct)
		logger.Error(errUpdateTbUserInfoStruct)
		return errUpdateTbUserInfoStruct
	}

	return nil
}

//UpdateNotZeroTbUserInfoStruct 更新
//如果入参ctx中没有dbConnection,使用defaultDao开启事务并最后提交
//如果入参ctx有dbConnection且没有事务,调用dbConnection.begin()开启事务并最后提交
//如果入参ctx有dbConnection且有事务,只使用不提交,有开启方提交事务
//但是如果遇到错误或者异常,虽然不是事务的开启方,也会回滚事务,让事务尽早回滚
func UpdateNotZeroTbUserInfoStruct(ctx context.Context, tbuserinfostruct *structs.TbUserInfoStruct) error {

	// tbuserinfostruct对象指针或主键Id不能为空
	if tbuserinfostruct == nil || (tbuserinfostruct.Id) < 1 {
		return errors.New("tbuserinfostruct对象指针或主键Id不能为空")
	}

	//匿名函数return的error如果不为nil,事务就会回滚
	_, errUpdateTbUserInfoStruct := zorm.Transaction(ctx, func(ctx context.Context) (interface{}, error) {

		//事务下的业务代码开始
		_, errUpdateTbUserInfoStruct := zorm.UpdateNotZeroValue(ctx, tbuserinfostruct)

		if errUpdateTbUserInfoStruct != nil {
			return nil, errUpdateTbUserInfoStruct
		}

		return nil, nil
		//事务下的业务代码结束

	})

	//记录错误
	if errUpdateTbUserInfoStruct != nil {
		errUpdateTbUserInfoStruct := fmt.Errorf("service.UpdateNotZeroTbUserInfoStruct错误:%w", errUpdateTbUserInfoStruct)
		logger.Error(errUpdateTbUserInfoStruct)
		return errUpdateTbUserInfoStruct
	}

	return nil
}

//DeleteTbUserInfoStructById 根据Id删除
//如果入参ctx中没有dbConnection,使用defaultDao开启事务并最后提交
//如果入参ctx有dbConnection且没有事务,调用dbConnection.begin()开启事务并最后提交
//如果入参ctx有dbConnection且有事务,只使用不提交,有开启方提交事务
//但是如果遇到错误或者异常,虽然不是事务的开启方,也会回滚事务,让事务尽早回滚
func DeleteTbUserInfoStructById(ctx context.Context, id string) error {

	//id不能为空
	if len(id) < 1 {
		return errors.New("id不能为空")
	}

	//匿名函数return的error如果不为nil,事务就会回滚
	_, errDeleteTbUserInfoStruct := zorm.Transaction(ctx, func(ctx context.Context) (interface{}, error) {

		//事务下的业务代码开始
		finder := zorm.NewDeleteFinder(structs.TbUserInfoStructTableName).Append(" WHERE id=?", id)
		_, errDeleteTbUserInfoStruct := zorm.UpdateFinder(ctx, finder)

		if errDeleteTbUserInfoStruct != nil {
			return nil, errDeleteTbUserInfoStruct
		}

		return nil, nil
		//事务下的业务代码结束

	})

	//记录错误
	if errDeleteTbUserInfoStruct != nil {
		errDeleteTbUserInfoStruct := fmt.Errorf("service.DeleteTbUserInfoStruct错误:%w", errDeleteTbUserInfoStruct)
		logger.Error(errDeleteTbUserInfoStruct)
		return errDeleteTbUserInfoStruct
	}

	return nil
}

//FindTbUserInfoStructById 根据Id查询信息
//ctx中如果没有dbConnection,则会使用默认的datasource进行无事务查询
func FindTbUserInfoStructById(ctx context.Context, id string) (*structs.TbUserInfoStruct, error) {
	//id不能为空
	if len(id) < 1 {
		return nil, errors.New("id不能为空")
	}

	//根据Id查询
	finder := zorm.NewSelectFinder(structs.TbUserInfoStructTableName).Append(" WHERE id=?", id)
	tbuserinfostruct := structs.TbUserInfoStruct{}
	has, errFindTbUserInfoStructById := zorm.QueryRow(ctx, finder, &tbuserinfostruct)

	//记录错误
	if errFindTbUserInfoStructById != nil {
		errFindTbUserInfoStructById := fmt.Errorf("service.FindTbUserInfoStructById错误:%w", errFindTbUserInfoStructById)
		logger.Error(errFindTbUserInfoStructById)
		return nil, errFindTbUserInfoStructById
	}

	if !has {
		return nil, nil
	}

	return &tbuserinfostruct, nil

}

//FindTbUserInfoStructList 根据Finder查询列表
//ctx中如果没有dbConnection,则会使用默认的datasource进行无事务查询
func FindTbUserInfoStructList(ctx context.Context, finder *zorm.Finder, page *zorm.Page) ([]structs.TbUserInfoStruct, error) {

	//finder不能为空
	if finder == nil {
		return nil, errors.New("finder不能为空")
	}

	tbuserinfostructList := make([]structs.TbUserInfoStruct, 0)
	errFindTbUserInfoStructList := zorm.Query(ctx, finder, &tbuserinfostructList, page)

	//记录错误
	if errFindTbUserInfoStructList != nil {
		errFindTbUserInfoStructList := fmt.Errorf("service.FindTbUserInfoStructList错误:%w", errFindTbUserInfoStructList)
		logger.Error(errFindTbUserInfoStructList)
		return nil, errFindTbUserInfoStructList
	}

	return tbuserinfostructList, nil
}
